import React, { Component } from 'react'
import PropTypes from 'prop-types'
import './index.css'

export default class Item extends Component {
  state = { mouse: false }

  static propTypes = {
    todo: PropTypes.object.isRequired,
    updateTodo: PropTypes.func.isRequired,
    deleteTodo: PropTypes.func.isRequired,
  }

  handleMouse = (flag) => {
    return () => {
      this.setState({
        mouse: flag,
      })
    }
  }

  handleCheck = (id) => {
    return (event) => {
      this.props.updateTodo(id,event.target.checked);
    }
  }

  handleDelete = (id) => {
    this.props.deleteTodo(id);
  }

  render() {
    const { todo } = this.props;
    const { mouse } = this.state;
    return (
      <li
        style={{ backgroundColor: mouse ? '#ddd' : 'white' }}
        onMouseEnter={this.handleMouse(true)}
        onMouseLeave={this.handleMouse(false)}
      >
        <label>
          <input
            type="checkbox"
            onChange={this.handleCheck(todo.id)}
            checked={todo.isCheck}
          />
          <span>{todo.name}</span>
        </label>
        <button onClick={() => { this.handleDelete(todo.id) }} className="btn btn-danger" style={{ display: mouse ? 'block' : 'none' }}>删除</button>
      </li>
    )
  }
}
